package com.my.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.my.entity.User;
import com.my.service.UserService;
import com.my.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api")
@CrossOrigin
public class UserApiController {

    @Autowired
    private UserService service;

    @PostMapping("user/update")
    public Rest update(@RequestBody User user) {
        if(StrUtil.isBlank(user.getPassword())) {
            user.setPassword(null);
        }else {
            user.setPassword(SecureUtil.md5(user.getId() + user.getPassword()));
        }
        if(service.updateById(user)) {
            return Rest.ok("修改成功！").setData("user", service.getById(user.getId()));
        }
        return Rest.ok("修改失败！");
    }

    @GetMapping("randUser")
    public Rest randUser(Long classId, Integer count) {
        return Rest.ok().setData("list", service.randStudent(classId, count));
    }

}
